package com.runccyy.web;

import com.runccyy.dao.model.UserPO;
import com.runccyy.exception.BizException;
import com.runccyy.service.UserService;
import com.runccyy.service.model.AuthInfo;
import com.runccyy.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

@Controller
@RequestMapping("/user")
@Api(value = "UserController", description = "用户相关操作")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private RedisUtil redisUtil;


    @RequestMapping("/get")
    @ResponseBody
    @ApiOperation(value = "get", notes = "根据id获取用户")
    public UserPO get(@ApiParam(value = "用户id", required = true) @RequestParam(name = "userId") Long id,
                      @ApiParam(value = "鉴权信息", required = true)@RequestHeader String auth) {
        if (!redisUtil.hasKey(auth)){
            throw new BizException("600","鉴权错误，请重新登录");
        }
        if (Objects.isNull(id)){
            return null;
        }
        return userService.getUserById(id);
    }

    @RequestMapping("/getByOpenId")
    @ResponseBody
    @ApiOperation(value = "getByOpenId", notes = "根据openId获取用户")
    public UserPO getByOpenId(@ApiParam(value = "用户id", required = true) @RequestParam(name = "openId") String openId,
                      @ApiParam(value = "鉴权信息", required = true)@RequestHeader String auth) {
        if (!redisUtil.hasKey(auth)){
            throw new BizException("600","鉴权错误，请重新登录");
        }
        if (Objects.isNull(openId)){
            return null;
        }
        return userService.getUserByOpenId(openId);
    }

    @RequestMapping("/save")
    @ResponseBody
    @ApiOperation(value = "save", notes = "添加用户")
    public Long save(@ApiParam(value = "用户对象", required = true)@RequestBody UserPO user,
                     @ApiParam(value = "鉴权信息", required = true)@RequestHeader String auth) {
        if (!redisUtil.hasKey(auth)){
            throw new BizException("600","鉴权错误，请重新登录");
        }
        if (Objects.isNull(user)){
            return null;
        }
        return userService.insertUser(user);
    }

    @GetMapping("/queryByNameAndDepartment")
    @ResponseBody
    @ApiOperation(value = "queryByNameAndDepartment", notes = "根据名称和部门查询用户")
    public UserPO getUserByNameAndDepartment(@ApiParam(value = "用户姓名", required = true)@RequestParam String name,
                                             @ApiParam(value = "用户部门", required = true)@RequestParam String department,
                                             @ApiParam(value = "用户姓名", required = true)@RequestHeader String auth) {
        if (!redisUtil.hasKey(auth)){
            throw new BizException("600","鉴权错误，请重新登录");
        }
        return userService.getUserByNameAndDepartment(name, department);
    }

    @GetMapping("/authorize")
    @ResponseBody
    @ApiOperation(value = "authorize", notes = "用户登录授权")
    public AuthInfo authorize(@ApiParam(value = "wx js_code", required = true)@RequestParam("jsCode") String jsCode){
        return userService.authorize(jsCode);
    }

}
